<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编号" prop="equipmentNumber">
        <el-input
          v-model="queryParams.equipmentNumber"
          placeholder="请输入设备编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="终端类型" prop="shipType">
        <el-select v-model="queryParams.terminalType" placeholder="请选择终端类型" clearable size="small">
          <el-option
            v-for="dict in shipTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="terminalList" row-key="id"
              :tree-props="{children: 'children'}">">
      <el-table-column label="设备名称" align="left" header-align="center" prop="equipmentName"/>
      <el-table-column label="设备编号" align="center" prop="equipmentNumber"/>
      <el-table-column label="终端类型" align="center" prop="terminalType" :formatter="terminalTypeFormat"/>
      <el-table-column label="绑定人" align="center" prop="userName" />
      <el-table-column label="日上线次数" align="center" prop="dayNum"/>
      <el-table-column label="总计上线次数" align="center" prop="totalNum"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div id="myChart"></div>
  </div>
</template>

<script>
  import {terminalInlineStatistics, terminalInlineChildrenStatistics} from "@/api/basicInformation/terminal";
  import echarts from "echarts";

  export default {
    name: "Terminal",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        shipTypeNow: "",
        // 总条数
        total: 0,
        // 终端注册表格数据
        terminalList: [],
        terminalChildrenList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 终端类型字典
        terminalTypeOptions: [],
        // 职位字典
        positionYOptions: [],
        // 巡护人员职位字典
        positionOptions: [],
        // 当前状态字典
        stateOptions: [],
        // 船舶类型字典
        shipTypeOptions: [],
        // 查询参数
        queryParams: {
          id: undefined,
          pageNum: 1,
          pageSize: 10,
          equipmentName: undefined,
          equipmentNumber: undefined,
          terminalType: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
      this.getDicts("terminal_type").then(response => {
        this.shipTypeOptions = response.data;
      });
      this.getDicts("terminal_type").then(response => {
        this.terminalTypeOptions = response.data;
      });
      this.getDicts("app_post").then(response => {
        this.positionYOptions = response.data;
      });
      this.getDicts("app_s_post").then(response => {
        this.positionOptions = response.data;
      });
    },
    methods: {
      load(tree, treeNode, resolve) {
        this.shipTypeNow = tree.shipType;
        terminalInlineChildrenStatistics({id: tree.id, shipType: tree.shipType}).then(response => {
          resolve(response.rows)
        });

      },
      /** 查询终端注册列表 */
      getList() {
        this.loading = true;
        terminalInlineStatistics(this.queryParams).then(response => {
          this.terminalList = response.rows;
          this.total = response.total;
          this.loading = false;

          let nameList = [];
          let dataList = [];
          for (var i = 0; i < response.rows.length; i++) {
            nameList.push(response.rows[i].equipmentName);
            dataList.push(response.rows[i].totalNum);
          }

          // 实例化echarts对象
          let myChartDrawer = echarts.init(document.getElementById('myChart'))

          // 绘制条形图
          var option = {
            color: ['#91c7ae'],
            tooltip: {
              trigger: 'axis',
              axisPointer: {
                type: 'line'
              }
            },
            title: {
              text: '终端上线情况统计',
              top: 5,
              left: 'center'
            },
            legend: {
              data: ['总计上线次数'],
              top: 30
            },
            // X轴
            xAxis: {
              data: nameList
            },
            // Y轴
            yAxis: {},
            // 数据
            series: [
              {
                name: '总计上线次数',
                type: 'bar',
                data: dataList,
                barWidth: 30
              }
            ]
          };

          myChartDrawer.setOption(option);
        });
      },
      // 终端类型字典翻译
      terminalTypeFormat(row, column) {
        return this.selectDictLabel(this.terminalTypeOptions, row.terminalType);
      },
      // 当前状态字典翻译
      stateFormat(row, column) {
        return this.selectDictLabel(this.stateOptions, row.state);
      },
      // 船舶类型字典翻译
      shipTypeFormat(row, column) {
        if (row.shipType != null) {
          return this.selectDictLabel(this.shipTypeOptions, row.shipType);
        }
        return null;
      },
      // 职位字典翻译
      positionFormat(row, column, cellValue, index) {
        if (row.position != null && row.shipType != null ) {
          if (row.shipType == "1")
            return this.selectDictLabel(this.positionYOptions, row.position);
          else
            return this.selectDictLabel(this.positionOptions, row.position);
        }
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有终端注册数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportTerminal(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
<style lang="scss" scoped>
  #myChart {
    width: 100%;
    min-height: 400px;
    clear: both;
    box-sizing: border-box;
    margin: 80px auto;
  }
</style>
